<?php

class TagsController extends AppController{
var $name = 'Tags';
var $components = array('Session', 'Auth');
//function beforeSave()
//    {
//    return true;
//    }

    function beforeFilter()
    {
        $this->Auth->allow('view');
        parent::beforeFilter();
    }    

function edit_description($id=null)
{

    if ($id) {
        $this->set('default', $id);
        $com= $this->Tag->find('first',array('conditions'=> array('Tag.tag'=> $id), 'fields' => 'Tag.description'));
        $this->set('default1', $com['Tag']['description']);
        }
    if (!empty($this->data)) {
//        
//        $this->data['Tag']['tag']
        $com1 =  $this->data['Tag']['description'];
        $this->Tag->updateAll( array('Tag.description' => "'$com1'"), array('Tag.tag' => $this->data['Tag']['tag']));  
        $this->redirect('/tags/view/'.$this->data['Tag']['tag'], true);
        }
  }

function view_description($id=null)
{
      $this->layout = 'none'; 
      $this->render('/elements/empty');
      $com= $this->Tag->find('first',array('conditions'=> array('Tag.tag'=> $id), 'fields' => 'Tag.description'));
      echo $com['Tag']['description'];
      
    }


function editparents($id=null)
{
    if ($id) {
        $this->set('default', $id);      
        $com3=$this->Tag->query("SELECT tag, id FROM `tags` WHERE id IN (SELECT parent_id FROM `tags_parents` WHERE `tag_id` = (SELECT `Tag`.`id` FROM `tags` AS `Tag` WHERE `Tag`.`tag` = '$id' LIMIT 1))"); 
        $beta="";
        foreach ($com3 as $val) {
           $beta.=$val['tags']['tag']." ";
        }
        $this->set('editparents',$beta);
        
     }
     if (!empty($this->data)) {
            $thistag=$this->data['Tag']['tag'];
            $com1=$this->Tag->find('first', array('conditions' => array('Tag.tag' => $this->data['Tag']['tag']),  'fields' => 'Tag.id', 'recursive'=>-1));
            $this->data['Tag']['id']=$com1['Tag']['id'];
            $this->Tag->query("DELETE FROM tags_parents WHERE tag_id=".$this->data['Tag']['id']);
             if (!$this->data['Tag']['id']) {
                 $this->Session->setFlash('Nonexistent tag');
                 return;
            }
            if (!$this->data['Parent']['tag']) {
                $this->Session->setFlash('Insert Sub Tags');
                 return;
            }
            $moreTags=explode(" ", $this->data['Parent']['tag']);
            unset($this->data['Tag']['tag']);
            unset($this->data['Parent']['tag']);
            $beta="";
            foreach ($moreTags as $tag) {
                $com= $this->Tag->find('first', array('conditions' => array('Tag.tag' => $tag),  'fields' => 'Tag.id', 'recursive'=>-1));
                $this->data['Parent']['id']=$com['Tag']['id'];
                if (!$this->data['Parent']['id']) {
                       unset($this->data['Parent']['id']);
                       $this->data['Parent']['tag']=$tag;
                        $this->Tag->saveAll($this->data);
                        unset($this->data['Parent']['tag']);
                }
                else
                 $this->Tag->saveAll($this->data);
             }
             
             $this->set('com2',$thistag);
             $this->set('com1',$this->data['Tag']['id']);
             $this->set('com',$moreTags);
//             $this->updateparent($com1);
//             if (isset($this->data['ajax']))
//                 $this->set('ajax',true);    
                     
                 $this->redirect('/tags/view/'.$thistag, true);
                 $this->Session->setFlash('Updated successfully');
        }
}


function setparent($id=null)
{
    if ($id) {
        $this->set('default', $id);      
        $com3=$this->Tag->query("SELECT tag, id FROM `tags` WHERE id IN (SELECT tag_id FROM `tags_parents` WHERE `parent_id` = (SELECT `Tag`.`id` FROM `tags` AS `Tag` WHERE `Tag`.`tag` = '$id' LIMIT 1))"); 
        $beta="";
        foreach ($com3 as $val) {
           $beta.=$val['tags']['tag']." ";
        }
        $this->set('setparent',$beta);
        
     }
     if (!empty($this->data)) {
            
            $parent=$this->data['Parent']['tag'];
            $com1=$this->Tag->find('first', array('conditions' => array('Tag.tag' => $this->data['Parent']['tag']),  'fields' => 'Tag.id', 'recursive'=>-1));
            $this->data['Parent']['id']=$com1['Tag']['id'];
            $this->Tag->query("DELETE FROM tags_parents WHERE parent_id=".$this->data['Parent']['id']);
             if (!$this->data['Parent']['id']) {
                 $this->Session->setFlash('Nonexistent parent tag');
                 return;
            }
            if (!$this->data['Tag']['tag']) {
                $this->Session->setFlash('Insert Sub Tags');
                 return;
            }
            $moreTags=explode(" ", $this->data['Tag']['tag']);
            unset($this->data['Tag']['tag']);
            unset($this->data['Parent']['tag']);
            $beta="";
            foreach ($moreTags as $tag) {
            
                $com= $this->Tag->find('first', array('conditions' => array('Tag.tag' => $tag),  'fields' => 'Tag.id', 'recursive'=>-1));
                $this->data['Tag']['id']=$com['Tag']['id'];
                if (!$this->data['Tag']['id']) {
//                        $temp= array ('Parent'=> array('id'=>$this->data['Parent']['id']),
//                                    'Tag' => array( 'tag' => $tag)
//                            );
                       unset($this->data['Tag']['id']);
                       $this->data['Tag']['tag']=$tag;
//                       $this->Session->setFlash('Nonexistent tag: '.print_r($this->data,true));
                        $this->Tag->saveAll($this->data);
                        unset($this->data['Tag']['tag']);
//                     $this->Session->setFlash('Nonexistent tag: '.$tag);
//                     return;
                }
                else
                 $this->Tag->saveAll($this->data);
             }
             
             $this->set('com2',$parent);
             $this->set('com1',$this->data['Parent']['id']);
             $this->set('com',$moreTags);
//             $this->updateparent($com1);
//             if (isset($this->data['ajax']))
//                 $this->set('ajax',true);    
                     
                 $this->redirect('/tags/view/'.$parent, true);
                 $this->Session->setFlash('Updated successfully');
        }
}
function updateparent($parent='') {
   // find all sub tags.
    // add all those sub tags to a post
    
}
function suggest()
{
     
      $this->layout = 'none'; 
      $this->render('/elements/empty');
//      echo "hi";
//                print_r($this->data);
            $x=0;
            $tags=explode(" ", strtolower(trim(file_get_contents('php://input'))));
             foreach ($tags as $tag)
                 {
//                      echo $tag." ";
//                      $x++;
                      $com=$this->Tag->query("SELECT tag, id FROM `tags` WHERE id IN (SELECT parent_id FROM `tags_parents` WHERE `tag_id` = (SELECT `Tag`.`id` FROM `tags` AS `Tag` WHERE `Tag`.`tag` = '$tag' LIMIT 1))"); 
//                      if (isset($com['Tag']['tag']))
                     
                      if (count($com)!=0)
                         foreach ($com as $tag1) {
                             if (!in_array($tag1['tags']['tag'],$tags))
                              echo $tag1['tags']['tag']." ";
                             }
                 }
            
    }

function treeTag(&$com, $id) {
    $com=$this->Tag->query("SELECT tag, id FROM `tags` WHERE id IN (SELECT tag_id FROM `tags_parents` WHERE `parent_id` = $id)");
    if (count($com) == 0)
        return false;
    foreach ($com as &$val) {
        if (!$this->treeTag($val['child'], $val['tags']['id']))
           unset($val['child']);
    }
    return true;
}

function view($id=null) {
//    $com1=$this->Tag->find('first', array('conditions' => array('Tag.tag' => $id),  'fields' => 'Tag.id', 'recursive'=>-1));
//    $id=$com1['Tag']['id'];
    
    $fort= $this->Tag->find('first',array('conditions'=> array('Tag.tag'=> $id), 'fields' => 'Tag.description'));
    $this->set('description', $fort['Tag']['description']);
    
    $com1 = $this->Tag->query("SELECT `Tag`.`id` FROM `tags` AS `Tag` WHERE `Tag`.`tag` = '$id' LIMIT 1");
    $com2=$com1[0]['Tag']['id'];
    $com=$this->Tag->query("SELECT tag, id FROM `tags` WHERE id IN (SELECT tag_id FROM `tags_parents` WHERE `parent_id` = $com2)"); 
    $beta="";
    foreach ($com as &$val) {
       $beta.=$val['tags']['tag']." ";
       if (!$this->treeTag($val['child'], $val['tags']['id']))
               unset($val['child']);
    }
    $parent=$this->Tag->query("SELECT tag FROM `tags` WHERE id IN (SELECT parent_id FROM `tags_parents` WHERE `tag_id`= $com2)");
    if ($parent)
        $this->set('parent', $parent);
     $this->set('setparent', $beta);
//,$parent[0]['tags']['tag']

    
//    $com=$this->Tag->query("SELECT tag_id FROM `tags_parents` WHERE `parent_id` = 14"); 
//    $com1=array();
//    foreach ($com as $val)
//        {
//         $com1['OR'][]['Tag.id']=$val['tags_parents']['tag_id'];
//         
////         $com1[]=$com2['Tag']['tag'];
//        }
////     $com1=$this->Tag->find('all', array('conditions' => $com1,  'fields' => 'Tag.tag', 'recursive'=>-1));   
        
    
//    $com=$this->Tag->find('all', array(
//        'conditions'=> array('Parent.id'=>$id), 
//        'recursive'=>-1, 
//        'joins'=> 
//        array('table' => 'tags_parents',  
//            'alias' => 'Parent',      
//            'type' => 'LEFT',      
//            'conditions' => array(     
//                'Parent.id = Tag.parent_id',     
//                ) 
//            )
//        ));
     $this->set('com1',$id);
   
    $this->set('com',$com);
}


function add() {
    //try modifying this!
    //Save the association
    $this->Session->setFlash(__('Welcome!.', true));
    $com= array();
    
    if (isset($this->data['Post'])) {
            $post="Post";
            $post1="post";
            }
    else {
            $post="Comment";
            $post1="comment";
            }
   
    $com[$post]=array('id'=>$this->data[$post]['id']);
    $this->Session->setFlash(__($this->data[$post]['Tag']['tag'], true));
    $com2=explode(" ", strtolower(trim($this->data['Tag']['tag'])));
    rsort($com2);
    $this->Tag->query("DELETE FROM ".$post1."s_tags WHERE ".$post1."_id='".$this->data[$post]['id']."'");
    foreach ($com2 as $value)
        {
            $com1=$this->Tag->findByTag($value);
            if ($com1!==false)
                $this->Tag->id=$com1['Tag']['id'];
            else
                $this->Tag->id=""; 
            $com['Tag']['tag']=$value;
//            $this->Tag->deleteAll(array('post_id' => 7));
  
            $this->Tag->save($com);
            $this->Tag->create();
        }
    if (!isset($this->data['ajax'])) 
        $this->redirect($this->referer(array('action'=>'index'), true));
}
}

?>
